// pages/visa/visa.js
var httpUtil = require('../../utils/httpUtil.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: true,
    flag1: true,
    ismore:true,
    city:{},
    sortInfo:[{
      sortName: '推荐排序',
      urlImg: '../images/sel-n.png',
      urlSelImg: '../images/sel-y.png',
    },
     {
       sortName: '销售由高到低',
       urlImg: '../images/sel-n.png',
       urlSelImg: '../images/sel-y.png',
      },
      {
        sortName: '信用由高到低',
        urlImg: '../images/sel-n.png',
        urlSelImg: '../images/sel-y.png',
      }
    ],
    text:{},
    curIdx:0,
    curIdx1: -1,
    curIdx2: -1,
    curIdx3: -1,
    odr: ['sort desc', 'sale_total desc','credit desc'],
    curIdx4: -1,
    curIdx5: -1,
    curr:0,
    addressInfo: ["不限", "北京签地","北京签地","北京签地","北京签地","北京签地","北京签地","北京签地"],
    stayInfo:["北京", "北京", "北京", "北京", "北京", "北京", "北京", "北京"],
    typeInfo: [],
    serverInfo: [],
    promiseInfo: [],
    searchTitle: [
      {
        searchName: '排序',
        searchBtn: '../images/down11.png',
        clearSearchBtn: '../images/down12.png',
      },
      {
        searchName: '送签地',
        searchBtn: '../images/down11.png',
        clearSearchBtn: '../images/down12.png',
      },
      {
        searchName: '筛选',
        searchBtn: '../images/shaixuan1.png',
        clearSearchBtn: '../images/shaixuan.png',
      }
    ],
    goodsList:[],
    currd:0 
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getName(options.id);
    this.getList(options.id);
    this.getAreaList(options.id);
    this.getAreaCityList(options.id);
    that.getTypeList();
    that.getGxList();
    that.getCnList();
    httpUtil.getHttp("/search/get/text"
      , { id: 1 }, function (res) {
        //将markdown内容转换为towxml数据
        let data = app.towxml.toJson(res.text, 'markdown');
        that.setData({
          text: data
        })
      })
  },
  //获取列表
  getList: function (id) {
    var that = this;
    var curInx1 = that.data.curIdx1;
    var curInx2 = that.data.curIdx2;
    var curInx3 = that.data.curIdx3;
    var curInx4 = that.data.curIdx4;
    var curInx5 = that.data.curIdx5;
    var addressInfoId = "";
    var stayInfoId = "";
    var visaName="";
    var promiseInfo="";
    var serverInfo = "";

     if (curInx1!=-1){
      addressInfoId = that.data.addressInfo[that.data.curIdx1].id;
    } 
  
     if (curInx2 != -1) {
      stayInfoId = that.data.stayInfo[that.data.curIdx2].id;
    }
      if (curInx3 != -1) {
      visaName = that.data.typeInfo[that.data.curIdx3];
    }
    if (curInx4 != -1) {
      serverInfo = that.data.serverInfo[that.data.curIdx4];
    }
    console.log(curInx5);
    console.log(promiseInfo);

    if (curInx5 != -1) {
      promiseInfo = that.data.promiseInfo[that.data.curIdx5];
    }
    console.log(that.data.promiseInfo[that.data.curIdx5]);

    httpUtil.getHttp("/goods_travel/list"
      , { city: id, 
        type: 0, 
      odr: that.data.odr[that.data.curIdx],
        area: addressInfoId,
        permanentArea: stayInfoId,
        visaName: visaName,
        gxService: serverInfo,
        cnService: promiseInfo
      }, function (res) {
      that.setData({
        goodsList: res
      })
    })
    that.setData({
      flag1: true
    })
  },
  //获取列表
  getAreaCityList: function (id) {
    var that = this;
    httpUtil.getHttp("/city/get/area"
      , { city: id }, function (res) {
        that.setData({
          addressInfo: res
        })
      })

      
  }, getTypeList: function (id) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/dict"
      , { name: "visaType" }, function (res) {
        console.log(res);
        var typeInfo=[];
        for(var i=0;i<res.length;i++){
          typeInfo.push(res[i].code);
        }
        that.setData({
          typeInfo: typeInfo
        })
      })
  }, getGxList: function (id) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/dict"
      , { name: "gx_service" }, function (res) {
        console.log(res);
        var serverInfo = [];
        for (var i = 0; i < res.length; i++) {
          serverInfo.push(res[i].code);
        }
        that.setData({
          serverInfo: serverInfo
        })
      })
  }, getCnList: function (id) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/dict"
      , { name: "cn_service" }, function (res) {
        var promiseInfo = [];
        for (var i = 0; i < res.length; i++) {
          promiseInfo.push(res[i].code);
        }
        that.setData({
          promiseInfo: promiseInfo
        })
      })
  },getAreaList: function (id) {
    var that = this;
    httpUtil.getHttp("/city/get/area"
      , { city: id }, function (res) {
        that.setData({
          stayInfo: res
        })
      })
  },
  //获取列表
  getName: function (id) {
    var that = this;
    httpUtil.getHttp("/city/get/" + id, {}, function (res) {
      that.setData({
        city: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  show: function () {

    this.setData({ flag: false })

  },
  //消失

  hide: function () {
    this.setData({ flag: true })

  },
  sortSel:function(e){
    var that = this;
    that.setData({
      curIdx: e.currentTarget.dataset.index,
      flag1: true,
    })
    this.getList(that.data.city.swcjGoodsCity.id);
  },
  addressSel: function (e) {
    var that = this;
    var searchTitle = that.data.searchTitle;

    searchTitle[1].searchName = that.data.addressInfo[e.currentTarget.dataset.index].name;

    that.setData({
      curIdx1: e.currentTarget.dataset.index,
      searchTitle: searchTitle,
      flag1: true,
    })
    that.getList(that.data.city.swcjGoodsCity.id);

  },

  staySel:function(e){
    var that = this;
    that.setData({
      curIdx2: e.currentTarget.dataset.index,
    })
    that.getList(that.data.city.swcjGoodsCity.id);

  },
  typeSel: function (e) {
    var that = this;
    that.setData({
      curIdx3: e.currentTarget.dataset.index,
    })
    that.getList(that.data.city.swcjGoodsCity.id);
  },
  serverSel: function (e) {
    var that = this;
    that.setData({
      curIdx4: e.currentTarget.dataset.index,
    })
  },
  promiseSel: function (e) {
    var that = this;
    that.setData({
      curIdx5: e.currentTarget.dataset.index,
    })
  },
  searchDetail:function(e){
    var that = this;
    that.setData({
      flag1: false,
      curr:e.currentTarget.dataset.index,
      currd:e.currentTarget.dataset.index,
    })
  },
  submitData:function(e){
    var that = this;
    that.setData({
      flag1: true,
    }) 
  },
  resetData:function(e){
      var that=this;
      that.setData({
        curIdx: -1,
        curIdx1: -1,
        curIdx2: -1,
        curIdx3: -1,
        curIdx4: -1,
        curIdx5: -1,
        flag1: true
      })
  },
  closeBox:function(){
    var that = this;
    that.setData({
      flag1: true,
    }) 
  },
  golookVisa:function(e){
    var id = e.target.dataset.id;
    if(id==undefined)
    {
      return;
    }
    console.log(id);
    wx.navigateTo({
      url: '../visaInfo/visaInfo?id=' + id
    })
  },
  isShowMore:function(){
    this.setData({
      ismore: !this.data.ismore
    })
  }
})